package com.ufsoft.ntb.table;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

import javax.swing.JScrollBar;

/**
 * MouseWheelHandler is an inner class which implements the MouseWheelListener interface. MouseWheelHandler responds to MouseWheelEvents by scrolling the JScrollPane appropriately. If the scroll pane's <code>isWheelScrollingEnabled</code> method returns false, no scrolling
 * occurs.
 * 
 * @see javax.swing.JScrollPane#isWheelScrollingEnabled
 * @see #createMouseWheelListener
 * @see java.awt.event.MouseWheelListener
 * @see java.awt.event.MouseWheelEvent
 * @since 1.4
 */
public class MouseWheelHandler implements MouseWheelListener {
	/**
	 * Called when the mouse wheel is rotated while over a JScrollPane.
	 * 
	 * @param e
	 *            MouseWheelEvent to be handled
	 * @since 1.4
	 */
	TablePane scrollpane;

	public MouseWheelHandler(TablePane tp){
		scrollpane=tp;
	}
	public void mouseWheelMoved(MouseWheelEvent e) {
		if (e.getScrollAmount() != 0) {
			JScrollBar toScroll = scrollpane.getVScrollBar2();
			int direction = 0;
			// find which scrollbar to scroll, or return if none
			if (toScroll == null || !toScroll.isVisible()) {
				toScroll = scrollpane.getHScrollBar2();
				if (toScroll == null || !toScroll.isVisible()) {
					toScroll = scrollpane.getVScrollBar();
					if (toScroll == null || !toScroll.isVisible()) {
						toScroll = scrollpane.getHScrollBar();
						if (toScroll == null || !toScroll.isVisible())
							return;
					}
					
				}
			}
			direction = e.getWheelRotation() < 0 ? -1 : 1;
			if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
				scrollByUnits(toScroll, direction, e.getScrollAmount());
			} else if (e.getScrollType() == MouseWheelEvent.WHEEL_BLOCK_SCROLL) {
				scrollByBlock(toScroll, direction);
			}
		}
	}
    static void scrollByBlock(JScrollBar scrollbar, int direction) {
        // This method is called from BasicScrollPaneUI to implement wheel
        // scrolling, and also from scrollByBlock().
	    int oldValue = scrollbar.getValue();
	    int blockIncrement = scrollbar.getBlockIncrement(direction);
	    int delta = blockIncrement * ((direction > 0) ? +1 : -1);

	    scrollbar.setValue(oldValue + delta);			
    }

    
    /*
     * Method for scrolling by a unit increment.
     * Added for mouse wheel scrolling support, RFE 4202656.
     */
    static void scrollByUnits(JScrollBar scrollbar, int direction,
                                   int units) {
        // This method is called from BasicScrollPaneUI to implement wheel
        // scrolling, as well as from scrollByUnit().
        int delta = units;

        if (direction > 0) {
            delta *= scrollbar.getUnitIncrement(direction);
        }
        else {
            delta *= -scrollbar.getUnitIncrement(direction);
        }

        int oldValue = scrollbar.getValue();
        int newValue = oldValue + delta;

        // Check for overflow.
        if (delta > 0 && newValue < oldValue) {
            newValue = scrollbar.getMaximum();
        }
        else if (delta < 0 && newValue > oldValue) {
            newValue = scrollbar.getMinimum();
        }
        scrollbar.setValue(newValue);
    }


}